const path = require('path');
const glob = require('globby');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

// 遍历除所有需要打包的文件路径
function getFilesEntries(config) {
    const fileList = glob.sync(config.pattern);
    return fileList.reduce(function (previous, current) {
        const filePath = path.parse(path.relative(config.src, current));
        const withoutSuffix = path.join(filePath.dir, filePath.name);
        previous[withoutSuffix] = path.resolve(__dirname, current);
        return previous
    }, {})
}

// 文件入口配置
const FILE_PATH = {
    css: {
        pattern: ['./src/css/*.css','./src/css/*.scss'],
        src: path.join(__dirname, './src/css'),
        dst: path.resolve(__dirname, 'public/css')
    },
    js: {
        pattern: ['./src/js/*.js','./src/js/modules/*.js'],
        src: path.join(__dirname, './src/js'),
        dst: path.resolve(__dirname, 'public/js')
    },
    ts: {
        pattern: ['./src/ts/*.ts'],
        src: path.join(__dirname, './src/ts'),
        dst: path.resolve(__dirname, 'public/js/modules')
    }
};

module.exports = [
    // 打包css
    {
        mode: "production",
        context: path.resolve(__dirname),
        entry: getFilesEntries(FILE_PATH.css),
        output: {
            path: FILE_PATH.css.dst,
            filename: '[name].css'
        },
        module: {
            rules: [
                {
                    test: /\.(scss|css)$/,
                    exclude: /node_modules/,
                    use: ExtractTextPlugin.extract({
                        use: [{
                                loader:'css-loader'
                            },
                            {
                                loader:'sass-loader'
                            },
                            'postcss-loader']
                    })
                }
            ]
        },
        plugins: [ new ExtractTextPlugin("[name].css") ]
    },
    //转换typescript
    /*
    {
        mode: "production",
        context: path.resolve(__dirname),
        entry: getFilesEntries(FILE_PATH.ts),
        output: {
            path: FILE_PATH.ts.dst,
            filename: "[name].js",
            library: "MA",
            libraryTarget: "umd"
        },
        module: {
            rules: [
                {
                    test: /\.ts$/,
                    exclude: /node_modules/,
                    use: ['babel-loader', 'ts-loader']
                }
            ]
        },
        plugins: [ new ExtractTextPlugin("[name].js") ]
    },
    * */
    // 打包js
    {
        mode: "production",
        context: path.resolve(__dirname),
        entry: getFilesEntries(FILE_PATH.js),
        output: {
            path: FILE_PATH.js.dst,
            filename: "[name].js",
        },
        optimization: {
            minimize: process.env.NODE_ENV === 'dev' ? false : true
        },
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    loader: "babel-loader",
                    query: {
                        compact: false
                    }
                }
            ]
        },
        plugins: [ new ExtractTextPlugin("[name].js") ]
    }
];